﻿// -----------------------------------------------------------------------
// <copyright file="XMLConfigSource.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NetFramework.Configuration.ConfigSource
{
    using System;
    using NetFramework.Serialization;
    using NetFramework.Diagnostics;
    using System.Xml;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class XMLConfigSource : ConfigSourceBase
    {
        private static XMLConfigSource instance;

        static XMLConfigSource()
        {
            instance = new XMLConfigSource();
        }

        public static XMLConfigSource Current
        {
            get
            {
                return instance;
            }
        }

        public XMLConfigSource() : base()
        {
        }

        protected override T GetConfigFromSource<T>(IConfigParameter parameter)
        {
            T config = default(T);

            if (parameter.Files.Length > 0)
            {
                if (parameter.Files.Length == 1)
                {
                    try
                    {
                        config = SerializerManager.XmlSerializer.FromFile<T>(parameter.Files[0]);
                    }
                    catch (Exception ex)
                    {
                        //ConfigThrowHelper.ThrowConfigException(ex, SR.ConfigFileNotResolved, cp.Files[0], typeof(T).FullName);
                        log.Error(ex);
                    }
                }
                else
                {
                    string xmlConent = MergeXmlFiles(parameter.Files);
                    config = SerializerManager.XmlSerializer.FromSerializedString<T>(xmlConent);
                }
            }

            return config;
        }

        public string MergeXmlFiles(string[] files)
        {
            Ensure.ArgumentNotNull(files, "files");

            XmlDocument doc = new XmlDocument();
            if (files.Length > 0)
            {
                string fileToMerge = string.Empty;
                for (int i = 0; i < files.Length; i++)
                {
                    fileToMerge = files[i];

                    try
                    {
                        if (doc.DocumentElement == null)
                        {
                            doc.Load(fileToMerge);
                        }
                        else
                        {
                            XmlDocument docTemp = new XmlDocument();
                            docTemp.Load(fileToMerge);
                            foreach (XmlNode node in docTemp.DocumentElement.ChildNodes)
                            {
                                XmlNode nodeTemp = doc.ImportNode(node, true);
                                doc.DocumentElement.AppendChild(nodeTemp);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex, "XMLConfigSource MergeXmlFiles error");
                    }
                }
            }

            return doc.OuterXml;
        }
    }
}
